// 1781. 所有子字符串美丽值之和
/**
 * @param {string} s
 * @return {number}
 */
// 双层循环
var beautySum = function (s) {
	let res = 0
	for (let i = 0; i < s.length; i++) {
		// 记录每个字符出现的次数
		const cnt = new Array(26).fill(0)
		let maxFreq = 0
		for (let j = i; j < s.length; j++) {
			cnt[s[j].charCodeAt() - 'a'.charCodeAt()]++
			maxFreq = Math.max(
				maxFreq,
				cnt[s[j].charCodeAt() - 'a'.charCodeAt()]
			)
			let minFreq = s.length
			for (let k = 0; k < 26; k++) {
				// 必须要有美丽值
				if (cnt[k] > 0) {
					minFreq = Math.min(minFreq, cnt[k])
				}
			}
			res += maxFreq - minFreq
		}
	}
	return res
}
const ret = beautySum('aabcb')
console.log('beautySum :>>', ret)
